package com.mall.controller;

import com.mall.common.constants.Constants;
import com.mall.common.resp.ResponseResult;
import com.mall.common.utils.FileUtils;
import com.mall.common.utils.Security;
import com.mall.controller.vo.UserVO;
import com.mall.entity.User;
import com.mall.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author xyl
 * @date 2021-02-16 17:34
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("获取当前用户信息")
    @GetMapping("/get")
    public ResponseResult<UserVO> getUserInfo(){
        return new ResponseResult<>(userService.getUserVoById(Security.getCurrentUserId()));
    }

    @ApiOperation("修改个人信息")
    @PostMapping("/modify")
    public ResponseResult modifyUserInfo(@RequestBody User user){
        return ResponseResult.toResponse(userService.modifyUserInfo(user));
    }

    @ApiOperation("上传头像")
    @PostMapping("/upload/avatar")
    public ResponseResult uploadAvatar(@RequestParam String base64Image){
        String s = FileUtils.Base64ToImage(Constants.NATIVE_IMAGE_PATH,base64Image);
        if(StringUtils.isEmpty(s)){
            return ResponseResult.error("修改头像失败!");
        }
        User user = new User();
        user.setUserId(Security.getCurrentUserId());
        user.setAvatar(Constants.VIRTUAL_IMAGE_PATH + s);
        userService.modifyUserInfo(user);
        return ResponseResult.success(Constants.VIRTUAL_IMAGE_PATH + s);
    }
}